home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / Games / MoofWars / MoofWars Encoder 8⁄15⁄96 / •Sources / PICTMask.cp < prev   
Encoding:
Text File  |  1996-08-19  |  6.6 KB  |  211 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #    PICTMask.cp
  4. #
  5. #    Author: Timothy Carroll
  6. #    Apple Developer Technical Support
  7. #    timc@apple.com
  8. #
  9. #    Modification History: 
  10. #
  11. #    8/15/96        TMC     Initial Release
  12. #
  13. #    Copyright © 1996 Apple Computer, Inc., All Rights Reserved
  14. #
  15. #
  16. #    You may incorporate this sample code into your applications without
  17. #    restriction, though the sample code has been provided "AS IS" and the
  18. #    responsibility for its operation is 100% yours.  However, what you are
  19. #    not permitted to do is to redistribute the source as "DSC Sample Code"
  20. #    after having made changes. If you're going to re-distribute the source,
  21. #    we require that you make it clear in the source that the code was
  22. #    descended from Apple Sample Code, but that you've made changes.
  23. #
  24. *************************************************************************************/
  25.  
  26. #include "Color Search Procs.h"
  27. #include "PICTMask.h"
  28.  
  29. OSStatus GeneratePICTMasks (short inputFileResNum, short outputFileResNum)
  30. {
  31.     OSStatus        theErr;
  32.  
  33.     UInt16            numPicts, loop;
  34.     
  35.     // we pass these to GetResInfo so that we can get the actual resource ID.
  36.     SInt16            resID;
  37.     ResType            resType;
  38.     Str255            resName;
  39.  
  40.     // Holds the last value on the resource chain since we change the top resource a lot
  41.     SInt16            saveResNum;
  42.     
  43.     // The current picture being worked on, along with its GWorld and Pixmap
  44.     PicHandle        thePicture = NULL;
  45.     GWorldPtr        pictGWorld = NULL;
  46.     PixMapHandle    pictPixMap = NULL;
  47.     OpenCPicParams    theParams; // used to build the output picture
  48.     Rect            pictRect, drawRect, sourceRect;
  49.     
  50.     // Saves port info while we draw into the GWorld
  51.     CGrafPtr        savePort;
  52.     GDHandle         saveDevice;
  53.     
  54.     SInt16            offset;
  55.     
  56.     Boolean            pictIsResource = false; // Determines if we call ReleaseResource or DisposeHandle
  57.     
  58.     // Save off the current resource chain so that we can restore it later
  59.     saveResNum = CurResFile();
  60.  
  61.     UseResFile (inputFileResNum);
  62.  
  63.  
  64.  
  65.     // First thing is to copy the color table resource to the output.
  66.     theErr = CopyResource (inputFileResNum, outputFileResNum,'clut', kAppColorTableResID);
  67.     FAIL_OSERR (theErr, "\pFailed to copy the color table to the destination file")
  68.     
  69.     // get the color table
  70.     gAppColorTable = GetCTable( kAppColorTableResID );
  71.     FAIL_NIL (gAppColorTable, "\pFailed to open the color table")
  72.  
  73.     // determine the number of icon resources
  74.     numPicts = Count1Resources( 'PICT' );
  75.     
  76.     // get each one,
  77.     for( loop = 1; loop <= numPicts; loop++ )
  78.     {
  79.     // *********************************************************************
  80.     // Load the PICT and get the resource information
  81.     // *********************************************************************
  82.         UseResFile (inputFileResNum);
  83.         pictIsResource = true;
  84.         thePicture = (PicHandle) Get1IndResource ('PICT', loop);
  85.         theErr = ResError();
  86.         FAIL_NIL (thePicture, "\pFailed to load the picture")
  87.         FAIL_OSERR (theErr, "\pFailed to load the picture")
  88.         
  89.         GetResInfo( (Handle) thePicture, &resID, &resType, resName );
  90.         theErr = ResError();
  91.         FAIL_OSERR( theErr, "\pFailed to get info on the resource")
  92.         
  93.     // *********************************************************************
  94.     // Create a GWorld, erase it, and draw our pictures and masks into it
  95.     // We'll have three images across, left to right, in the GWorld
  96.     // *********************************************************************
  97.         pictRect = (**thePicture).picFrame;
  98.         OffsetRect (&pictRect, -pictRect.left, -pictRect.top);
  99.         drawRect = pictRect;
  100.         pictRect.right *= 3;
  101.         offset = drawRect.right;
  102.     
  103.         theErr = NewGWorld(&pictGWorld, kPreferredDepth, &pictRect, gAppColorTable, NULL, 0);
  104.         FAIL_OSERR (theErr, "\pFailed to allocate the GWorld")
  105.         FAIL_NIL (pictGWorld, "\pFailed to allocate the GWorld")
  106.     
  107.         pictPixMap  = GetGWorldPixMap(pictGWorld);
  108.         FAIL_NIL (pictPixMap, "\pCouldn't get the pixmap")
  109.         FAIL_FALSE ( LockPixels(pictPixMap), "\pCouldn't lock the pixmap")
  110.         
  111.         GetGWorld (&savePort, &saveDevice);
  112.         SetGWorld (pictGWorld, NULL);
  113.     
  114.         EraseRect (&pictRect);
  115.         DrawPicture (thePicture, &drawRect);
  116.         
  117.         sourceRect = drawRect;
  118.         
  119.         drawRect.left +=offset;
  120.         drawRect.right +=offset;
  121.         
  122.         // The top left corner of the picture is always assumed to be the Mask color!
  123.         // We set our mask color and insert our search proc, then we copy the mask.
  124.         
  125.         GetCPixel (0,0, &gMaskColor);
  126.         AddSearch (MaskSearchProcUPP);
  127.         CopyBits ((BitMap *) *pictPixMap, (BitMap *) *pictPixMap,
  128.                 &sourceRect, &drawRect, srcCopy, NULL);
  129.         DelSearch (MaskSearchProcUPP);
  130.         
  131.         sourceRect = drawRect;
  132.         drawRect.left+=offset;
  133.         drawRect.right+=offset;
  134.         
  135.         // The second is just a copy of the first.  No search is necessary
  136.         CopyBits ((BitMap *) *pictPixMap, (BitMap *) *pictPixMap,
  137.         &sourceRect, &drawRect, srcCopy, NULL);
  138.  
  139.     // *********************************************************************
  140.     // We're done with the old picture, so we can release it and build the
  141.     // new picture to add to the output file.
  142.     // *********************************************************************
  143.         ReleaseResource ((Handle) thePicture);
  144.         thePicture = NULL;
  145.     
  146.         pictIsResource = false;
  147.         
  148.         theParams.srcRect = pictRect;
  149.         theParams.hRes = 0x00480000;
  150.         theParams.vRes = 0x00480000;
  151.         theParams.version = -2;
  152.         theParams.reserved1 = 0;
  153.         theParams.reserved2 = 0;
  154.     
  155.         thePicture = OpenCPicture (&theParams);
  156.         CopyBits ((BitMap *) *pictPixMap, (BitMap *) *pictPixMap, &pictRect, &pictRect, srcCopy, NULL);
  157.         ClosePicture();
  158.         theErr = QDError();
  159.         FAIL_NIL (thePicture, "\pFailed to create the new picture")
  160.         FAIL_OSERR (theErr, "\pFailed to create the new picture")
  161.         
  162.         SetGWorld (savePort, saveDevice);
  163.     
  164.     // *********************************************************************
  165.     // Add the new PICT to the resource file
  166.     // *********************************************************************
  167.     // We're done with the GWorld
  168.         DisposeGWorld (pictGWorld);
  169.         pictGWorld = NULL;
  170.     
  171.         UseResFile (outputFileResNum);
  172.         AddResource( (Handle) thePicture, 'PICT', resID, resName );
  173.         theErr = ResError();
  174.         FAIL_OSERR (theErr,"\pFailed to add the resource to the output file")
  175.     
  176.         pictIsResource = true;
  177.     
  178.         WriteResource( (Handle) thePicture );
  179.         theErr = ResError();
  180.         FAIL_OSERR (theErr,"\pFailed to write the resource to the output file")
  181.     
  182.         ReleaseResource( (Handle) thePicture );
  183.         thePicture = NULL;
  184.     }
  185.     
  186.     goto cleanup;
  187.     
  188. error:
  189.     
  190.     if (theErr == noErr)
  191.         theErr = paramErr;
  192.         
  193. cleanup:
  194.     
  195.     UseResFile (saveResNum);
  196.     
  197.     if (pictGWorld != NULL)
  198.         DisposeGWorld (pictGWorld);
  199.     
  200.     if (thePicture != NULL)
  201.         if (pictIsResource)
  202.             ReleaseResource ((Handle) thePicture);
  203.         else
  204.             DisposeHandle ((Handle) thePicture);
  205.     
  206.     if (gAppColorTable != NULL)
  207.         DisposeCTable (gAppColorTable);
  208.     gAppColorTable = NULL;
  209.     return theErr;
  210. }
  211.